package com.promotores;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class GestorZona extends CreaTablas{
	CreaTablas db;
	// tipoPromocion table name
	private static final String TABLE_ZONA = "Zona";
    
	// tipoPromocion Table Columns names
	private static final String KEY_ID_ZONA = "idZona";
	private static final String KEY_DESCRIPCION = "descripcion";
	private static final String KEY_AFORO = "aforo";
	private static final String KEY_FARE = "fare";
	private static final String KEY_NOMBRE = "Nombre";
	private static final String KEY_ID_EVENTO = "idEvento";
	private static final String KEY_FLAG_SAVED = "FlagSaved";
	private static final String KEY_DELETED = "deleted";
	private static final String KEY_CREATED_AT = "created_at";
	private static final String KEY_UPDATED_AT = "updated_at";
	
	public GestorZona(Context context) {
		super(context);
		db = new CreaTablas(context);
	}
	/**
	 * All CRUD(Create, Read, Update, Delete) Operations
	 */

	// Adding new tipoPromocion
	public void addZona(BeanZona zona) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_ID_ZONA, zona.getId());
		values.put(KEY_ID_EVENTO, zona.getIdevento());
		values.put(KEY_AFORO, zona.getAforo());
		values.put(KEY_FARE, zona.getFare());
		values.put(KEY_NOMBRE, zona.getNombre());
		values.put(KEY_DESCRIPCION, zona.getDescripcion());
		values.put(KEY_FLAG_SAVED, zona.getSaved());
		values.put(KEY_DELETED, zona.getDeleted());
		values.put(KEY_CREATED_AT, zona.getCreated_at());
		values.put(KEY_UPDATED_AT, zona.getUpdated_at());
		
		// Inserting Row
		db.insert(TABLE_ZONA, null, values);
		//db.close(); // Closing database connection
	}

		

	// Getting All tipoPromocion
	public ArrayList<BeanZona> getAllZonaByEvento(int id) {
		ArrayList<BeanZona> listaZonas = new ArrayList<BeanZona>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_ZONA + " WHERE " + KEY_ID_EVENTO + " = " + id;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				BeanZona zona = new BeanZona();
				zona.setIdevento(id);
				zona.setId(Integer.parseInt(cursor.getString(1)));
				zona.setNombre(cursor.getString(2));
				zona.setDescripcion(cursor.getString(3));
				zona.setFare(Double.parseDouble(cursor.getString(4)));
				zona.setAforo(Integer.parseInt(cursor.getString(5)));
				zona.setSaved(cursor.getString(6));
				zona.setDeleted(cursor.getString(7));
				zona.setCreated_at(cursor.getString(8));
				zona.setUpdated_at(cursor.getString(9));
				// Adding tipoPromocion to list
				listaZonas.add(zona);
			} while (cursor.moveToNext());
		}
		cursor.close();
		db.close();
		// return Zone list
		return listaZonas;
	}

	// Updating single tipoPromocion
	public void updateZona(BeanZona zona) {
		SQLiteDatabase db = this.getWritableDatabase();
		String consulta = "UPDATE " + TABLE_ZONA 
						  + " SET " + KEY_NOMBRE + " = " 
						  + zona.getNombre() + ','
						  + KEY_AFORO + " = " 
						  + zona.getAforo() + ','
						  + KEY_FARE + " = "
						  + zona.getFare()
						  + " WHERE " + KEY_ID_ZONA + " = " + zona.getId() + " AND " + KEY_ID_EVENTO + " = " + zona.getIdevento()+" ;";
		db.execSQL(consulta);
		db.close();
	}

	

	public void syncData() {
		// TODO Auto-generated method stub
		
	}
}
